package com.ithiema;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] a = {3,4,2,5,6,8,9,7,1};
        //int[] a = {1,2,3,4,5,6,7,8,9};
        bubble(a);
    }
    public static void swap(int[] a ,int i,int j){
        int t =a[i];
        a[i]=a[j];
        a[j]=t;
    }
    public static void bubble(int[] a){
        int n = a.length-1;
        while(true){
            int last = 0;
            for (int i = 0; i < n; i++) {
                System.out.println("比较次数:"+i);
                if(a[i]>a[i+1]){
                    swap(a,i,i+1);
                    last = i ;
                }
            }
            n = last;
            System.out.println("冒泡比较"+ Arrays.toString(a));
            if(n == 0){
                break;
            }
        }
    }

}
